package zhoufu.sz.sanxiao_check.ui.basic;

import android.content.DialogInterface;
import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityPlaceChangeBinding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.presents.PlaceChangePresenter;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/29
 * 场所变更
 */
public class PlaceChangeActivity extends BaseMsgActivity
        implements BasicMsgItem2Contract.View<IDModel>
{
    ActivityPlaceChangeBinding _binding;

    @Inject
    PlaceChangePresenter _presenter;

    @Override protected void initInject()
    {
        setTitle(R.string.title_place_change);
        LoginComponet componet = DaggerLoginComponet.builder().appComponent(getAppComponent())
                .activityModule(getActivityModule()).build();
        componet.inject(this);
        _presenter.bindView(this);
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        _binding = (ActivityPlaceChangeBinding) binding;
        StreetInfoModel infoModel = new StreetInfoModel();
        infoModel.setRID(mInfoModel.getRID());
        infoModel.setSID(mInfoModel.getSID());
        infoModel.setType("1");
        _binding.setModel(infoModel);
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_place_change;
    }


    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_save )
        {
            new AlertDialog.Builder(this).setMessage("被变更的场所将不再显示,确定吗?").setNegativeButton(
                    R.string.dialog_ok, new DialogInterface.OnClickListener()
                    {
                        @Override public void onClick(DialogInterface dialog, int which)
                        {
                            _binding.getModel()
                                    .setType(_binding.mRgType.getCheckedRadioButtonId()
                                                     == R.id.mRbType1 ? "1" :
                                             _binding.mRgType.getCheckedRadioButtonId()
                                                     == R.id.mRbType2 ? "2" :
                                             "3");
                            _presenter.update(_binding.getModel());
                        }
                    }).setOnCancelListener(new DialogInterface.OnCancelListener()
            {
                @Override public void onCancel(DialogInterface dialog)
                {
                    dialog.dismiss();
                }
            }).create().show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override public String SID()
    {
        return mInfoModel.getSID();
    }

    @Override public void setModel(IDModel baseModel)
    {

    }

    @Override public void updateSuccese()
    {
        UIUtils.showToast(this, "变更成功");
        finish();
    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }

    @Override protected void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }
}
